package com.company.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Semaphore;

/**
 * @description: 同步机制3： 多资源控制Semaphore
 * @author: ruoan
 * @date: 2020/12/10 23:50
 */
@Slf4j
public class SemaphoreDemo {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(3);
        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                try {
                    semaphore.acquire();
                    log.info(Thread.currentThread().getName()+"获取到了资源1/3的使用机会");
                    Thread.sleep(3000); // 模拟业务
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            },String.valueOf(i)).start();
        }


    }

}
